<?php
/*
 * Created on 28.09.2007 by Nikolai Plath
 *
 * * @file:com_bindex.php
 */
defined( '_VALID_MOS' ) or die( 'Restricted access' );

error_reporting(E_ALL); //TODO ?

require_once( $mainframe->getPath( 'front_html' ) );
require_once( $mainframe->getPath( 'class' ) );

require_once( dirname (__FILE__) . '/bi_config.class.php' );
require_once( dirname (__FILE__) . '/bi_template.class.php' );
require_once( dirname (__FILE__) . '/gmap.class.php' );

# TODO soon obsolete... NiK
require_once( dirname (__FILE__) . '/bindex.helper.php' );

#debugging
require_once( dirname (__FILE__) . '/debug.php' );
include_once( dirname (__FILE__) . '/debuglib_v4.php');

#get language file
if (file_exists( dirname (__FILE__) .'/language/'.$mosConfig_lang.'.php' ) )
{ include_once( dirname (__FILE__) .'/language/'.$mosConfig_lang.'.php' );
} else
{ include_once( dirname (__FILE__) .'/language/english.php' );
}

$HTML 		= new HTML_BIndex;
$biConfig 	= new biConfig;
$GMap			= new gmap;

$biConfig->AppName 			= 'B-Index';
###$biConfig->AppVersion comes from database..
$biConfig->AppPath			= 'components/com_bindex/';
$biConfig->templatePath		= 'components/com_bindex/templates/';

//api http://b-index.der-beta-server.de //// ABQIAAAAIvhWU2DMTj5nBDSD-hPeKRQkEc2iS4vD7yudWGERc4HSNb5SZBQtzi1wtGhvr214FS6JW-t7qkjQQw
# wir lesen die configuration.php ein, um die config sprache festzustellen,
# da sie durch den Fish geaendert wird
$biConfig->act_language = $mosConfig_lang;//storing actual
include(dirname (__FILE__) . '/../../configuration.php');
$biConfig->ini_language = $mosConfig_lang;//storing default
$mosConfig_lang = $biConfig->act_language;//writing it back

$biConfig->absolutePath		= $mosConfig_absolute_path; //hardening ?
$biConfig->liveSite			= $mosConfig_live_site; 	//hardening ?

$HTML->loadJS_Tooltips();

#get languages with joomfish
if( !isset( $_JOOMFISH_MANAGER) )
{
//	include($biConfig->absolutePath.'administrator/components/com_joomfish/joomfish.class.php');
//	include($biConfig->absolutePath.'includes/sef.php');
	$_JOOMFISH_MANAGER = new JoomFishManager;
	if( !isset( $_JOOMFISH_MANAGER) ) echo '<h2>*TR* BI_NO_JOOMFISH</h2>';
//	return;
}// TODO i think we should include the joomfishcode..
//global $_JOOMFISH_MANAGER;
$langActive = $_JOOMFISH_MANAGER->getActiveLanguages();

#init debug
$DBG = new nikDebug;
$DBG->active = $biConfig->DEBUG;

$DBG->out('REQUEST');

/** Custom Stylesheet App */
$mainframe->addCustomHeadTag('<link rel="stylesheet" type="text/css" media="all" href="'.$biConfig->templatePath.'app/'.$biConfig->templateApp.'.css" />');

$mainframe->addCustomHeadTag('<link rel="stylesheet" type="text/css" media="all" href="components/com_bindex/bindex.css" />');

/** Custom JS */
$mainframe->addCustomHeadTag('<script type="text/javascript" src="'.$biConfig->liveSite.'/components/com_bindex/includes/js/bindex_js.js"></script>' );

$selected 	= strval( mosGetParam( $_REQUEST, 'order', '' ) );
$filter 	= stripslashes( strval( mosGetParam( $_REQUEST, 'filter', '' ) ) );
$limit 		= intval( mosGetParam( $_REQUEST, 'limit', $biConfig->list_limit ) );
$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );
$id_bereich = intval( mosGetParam( $_REQUEST, 'id_bereich', 0 ) );
$id_stadt	= intval( mosGetParam( $_REQUEST, 'id_stadt', 0 ) );

$from_task	= strval( mosGetParam( $_REQUEST, 'from_task', '' ) );

$Bid		= intval( mosGetParam( $_REQUEST, 'bid', 0 ) );

$template	= strval( mosGetParam( $_REQUEST, 'template', '' ) );
$preview	= strval( mosGetParam( $_REQUEST, 'preview', '' ) );

$help		= strval( mosGetParam( $_REQUEST, 'help', '' ) );

#google icon palette view no
$pal		= intval( mosGetParam( $_REQUEST, 'pal', 0 ) );

$popup		= intval( mosGetParam( $_REQUEST, 'popup', 0 ) );

#mode used for various
$mode		= intval( mosGetParam( $_REQUEST, 'mode', 0 ) );

if( $task == '' ) $task = 'bindex';

######################################## HEADER #########################################
if( $task != 'template_preview'
 && $task != 'show_gicons'
 && $task != 'help' )
{
	$HTML->biHeader(BI_TITEL);
	$HTML->biMenu();
}

switch( $task )
{
	### SAVE handlers first
	
	case 'save_config':
		if( ! canEdit() )
		{ 	$HTML->msgError( _NOT_AUTH );
		} else	
		{
			$biConfig->getConfigFromRequest();

			if( ! $biConfig->biConfigStore() )
			{ $HTML->msgError( BI_SETTINGS_NOT_SAVED );
			} else
			{ mosRedirect('index.php?option=com_bindex&amp;Itemid='.$Itemid.'&amp;task=save_config_ok');
			} //if
		} //if
		
	break;
	case 'save_config_ok':
		$HTML->msgInfo( BI_SETTINGS_SAVED );
		( canEdit() ) ? editConfig( $Bid ) : $HTML->msgError(_NOT_AUTH);
	break;
	case 'save_bereich':
		if( ! canEdit() )
		{ $HTML->msgError(_NOT_AUTH);
		} else
		{ ( saveBereich( $Bid ) ) ? $HTML->msgInfo( BI_ITEM_SAVED ) : $HTML->msgInfo( BI_ITEM_NOT_SAVED );
			showBereiche( $limitstart, $limit);
		} //if
	break;
	case 'save_stadt':
		if( !canEdit() )
		{ $HTML->msgError(_NOT_AUTH);
		} else
		{	( saveStadt( $Bid , $id_bereich) ) ? $HTML->msgInfo( BI_ITEM_SAVED ) : $HTML->msgError( BI_ITEM_NOT_SAVED );
			showStaedte( $limitstart, $limit );
		} //if
	break;
	case 'save_template':
		if( !canEdit() )
		{ $HTML->msgError(_NOT_AUTH);
		} else
		{
			if( $preview == 'yes' )
			{
				if( ! bindex::saveTemplate('preview', $mode ) )  $HTML->msgError( BI_ITEM_NOT_SAVED );
				editTemplate( $template, $mode, $preview );
			} else
			{
				echo'saving...not yet';
			} //if
		} //if
	break;
	case 'save':
		if( !canEdit() )
		{ $HTML->msgError(_NOT_AUTH);
		} else
		{
			( saveBindex() ) ? $HTML->msgInfo( BI_ITEM_SAVED ) : $HTML->msgInfo( BI_ITEM_NOT_SAVED );
			$DBG->query();
			if( $from_task == 'trans_status' )
			{ showTransStatus( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
			} else
			{ showBIndex( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
			}
		} //if
	break;
	
	### DELETE HANDLERS
	
	case 'kill':
		$HTML->msgError(BI_NOT_READY);
		showBIndex( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
	break;
	
	#CATEGORIES
	
	case 'cats':
		#showing subcat menu
		if( ! canEdit() ) $HTML->msgError(_NOT_AUTH);
	break;
	
	#Bereiche
	
	case 'bereiche':
		( canEdit() ) ? showBereiche( $limitstart, $limit ) : $HTML->msgError(_NOT_AUTH);
	break;
	case 'edit_bereich':
		( canEdit() ) ? editBereich( $Bid ) : $HTML->msgError(_NOT_AUTH);
	break;

	#Staedte
	
	case 'staedte':
		( canEdit() ) ? showStaedte( $limitstart, $limit ) : $HTML->msgError(_NOT_AUTH);
	break;
	case 'edit_stadt':
		( canEdit() ) ? editStadt( $Bid ) : $HTML->msgError(_NOT_AUTH);
	break;
	
	#ITEMS
	
	case 'view':
		showBIndexItem( $Bid, $popup );
	break;
	case 'edit':
		( canEdit() ) ? editBIndex( $Bid, $from_task ) : $HTML->msgError(_NOT_AUTH);
	break;
	
	#translation
	
	case 'trans_status':
		( canEdit() ) ? showTransStatus( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt ) : $HTML->msgError(_NOT_AUTH);
	break;
	
	#Settings
	
	case 'edit_config':
		( canEdit() ) ? editConfig() : $HTML->msgError(_NOT_AUTH);
	break;
	
	#Templates
	
	case 'templates':
		( canEdit() ) ? editTemplate( $template, $mode, $preview ) : $HTML->msgError(_NOT_AUTH);
	break;
	case 'template_preview': #this one goes into iframe
		( canEdit() ) ? previewTemplate( $template, $mode ) : $HTML->msgError(_NOT_AUTH);
	break;
	
	#SEARCH
	
	case 'search':
		$HTML->msgInfo( BI_NOT_READY );
		$HTML->formSearch($id_bereich, $id_stadt);
		showBIndex( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
	break;
	
	#GOOGLE related..
	case 'map':
//		$HTML->msgInfo( BI_NOT_READY );
		$GMap->g_map2( $id_bereich, $id_stadt);
	break;
	case 'show_gicons': // as a popup
		$GMap->drawGOG_IconPalette( $pal );
	break;

	### HELP !
	
	case 'help':
		$HTML->help( $help, $task );
	break;
	
	##### T E S T
	
	case 'test':
	echo 'aaaaa';
		test_ajax();
#		test_xgmap();
#		test_popup();
	break;
	
	case 'bindex':
	default:
		showBIndex( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
	break;
}//switch


#########  FOOTER
if( $task != 'template_preview' && ! $popup )
{ $HTML->NiK_footer();
} #######################################################################################################


/*
 * FUNCTIONS
 */

function editTemplate ( $template, $mode, $preview )
{
	global $HTML;

	##DEMO
	$HTML->msgAlert('Demo Mode');
	
	switch( $mode )
	{
		case 2:
			$path = 'directory/';
		break;
		case 3:
			$path = 'app/';
		break;
		case 1:
		default:
			$path = 'item/';
			$mode = 1;
		break;
	} //switch
	$biTemplate = new bi_template( $path, $template, $mode, $HTML);
	$biTemplate->edit();
	
} //function

function previewTemplate ( $template, $mode )
{
	global $HTML;
	switch( $mode )
	{
		case 2:
			$path = 'directory/';
		break;
		case 3:
			$path = 'app/';
		break;
		case 1:
		default:
			$path = 'item/';
			$mode = 1;
		break;
	} //switch
	$biTemplate = new bi_template( $path, $template, $mode, $HTML);

	$biTemplate->preview();
	
} //function

function showTransStatus ( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt )
{
	//showBereiche();
	//showStaedte();
	showBIndexTransStatus( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt );
} //function


function showBereiche( $limitstart = 0, $limit = 0 )
{
	global $database;
	global $HTML, $biConfig;
global $DBG;
	
	require_once( $biConfig->absolutePath . '/includes/pageNavigation.php' );
	$query = "SELECT *" .
			"\n FROM #__bindex_bereiche" .
			"\n WHERE `published` = '1'";
	$database->setQuery($query);
	
	$tmp = $database->loadObjectList();
	$total = count( $tmp );
	
	$limit = $limit ? $limit : $biConfig->list_limit;
	if ( $total <= $limit ) $limitstart = 0;

	$database->setQuery( $query, $limitstart, $limit );

	$bereiche_ids = $database->loadObjectList();
$DBG->query($query);
	$pageNav = new mosPageNav( $total, $limitstart, $limit );

	$HTML->listBereiche( $bereiche_ids, $pageNav, $limitstart, $limit );
} //function

function editBereich( $Bid )
{
	global $HTML;
	if( $Bid > 0 ) $HTML->formBereich( $Bid );
} //function

function saveBereich ( $Bid )
{
	global $database;
	global $langActive;
	global $biConfig;
global $DBG;

	$bereich = new bereich;

		$icon_pal 	= intval( 	mosGetParam( $_REQUEST, 'gicon_pal', 0 ) );
		$icon_no 	= intval( 	mosGetParam( $_REQUEST, 'gicon_no', 0 ) );
		$icon_name 	= strval( 	mosGetParam( $_REQUEST, 'gicon_name', 0 ) );
	
	if( $Bid == 0 )
	{//neu
		$name 		= 			mosGetParam( $_REQUEST, 'name_'.$biConfig->ini_language, '' );
		
		if( $name == '' ) return;
		
		$query = "INSERT INTO `#__bindex_bereiche`" .
				"\n ( `published`, `icon_pal`, `icon_no`, `icon_name` )" .
				"\n VALUES" .
				"\n ( '1', " .
				"\n '".$icon_pal."' ," .
				"\n '".$icon_no."', " .
				"\n '".$icon_name."'" .
				"\n )";
		$database->setQuery($query);
		$database->query();
		if ($database->getErrorNum())
		{
			echo $database->stderr();
			return FALSE;
		}
		
		$new_id = mysql_insert_id();
		$query = "INSERT INTO `#__bindex_bereiche_data`" .
				"\n ( `id_bereich`, `language`, `value` )" .
				"\n VALUES" .
				"\n ( '$new_id', '".$biConfig->ini_language."', '$name' )";
		$database->setQuery($query);
		$database->query();
$DBG->query($query);
		if ($database->getErrorNum())
		{
			echo $database->stderr();
			return FALSE;
		}
		
		return TRUE;
		
	} else
	{ #speichern
		$query = "UPDATE #__bindex_bereiche" .
				"\n SET" .
				"\n `icon_pal` = '".$icon_pal."', " .
				"\n `icon_no` = '".$icon_no."', " .
				"\n `icon_name` = '".$icon_name."', " .
				"\n `published` = '1'" .
				"\n WHERE `id` = '".$Bid."'";
		$database->setQuery($query);
		$database->query();
$DBG->query($query);
		
		foreach( $langActive as $language )
		{
	
			$name 		= 			mosGetParam( $_REQUEST, 'name_'.$language->code, '' );
	
			if( $name != '' )
			{
				$query = "SELECT * FROM `#__bindex_bereiche_data`" .
						"\n WHERE `id_bereich` = '$Bid'" .
						"\n AND `language` = '".$language->code."'";
				$database->setQuery($query);
				$res = $database->loadResult();
$DBG->query($query);
				if( $res )
				{//bereits vorhanden
					$query = "UPDATE `#__bindex_bereiche_data`" .
							"\n SET" .
							"\n `value` = '$name'" .
							"\n WHERE" .
							"\n `id_bereich` = '$Bid'" .
							"\n AND" .
							"\n `language` = '".$language->code."'";
					$database->setQuery($query);
					$database->query();
$DBG->query($query);
					if ($database->getErrorNum())
					{
						echo $database->stderr();
						return FALSE;
					}
				} else
				{//neu
					$query = "INSERT INTO `#__bindex_bereiche_data`" .
							"\n ( `id_bereich`, `language`, `value` )" .
							"\n VALUES" .
							"\n ( '$Bid', '".$language->code."', '$name' )";
					$database->setQuery($query);
					$database->query();
$DBG->query($query);
					if ($database->getErrorNum())
					{
						echo $database->stderr();
						return FALSE;
					}
				} //if
			} //if
		}//foreach
		
		return TRUE;
		
	} //if
} //function

function showStaedte( $limitstart = 0, $limit = 0 )
{
	global $database;
	global $HTML, $biConfig;
global $DBG;	
	$query = "SELECT * FROM #__bindex_staedte WHERE `published` = '1'";
	$database->setQuery($query);
	$tmp = $database->loadObjectList();
	$total = count( $tmp );
	
	if ($limit == 0) $limit = $biConfig->list_limit;
	if ( $total <= $limit ) $limitstart = 0;

	$database->setQuery( $query, $limitstart, $limit );
	$ids = $database->loadObjectList();
$DBG->query($query);

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );

	$HTML->listStaedte( $ids, $pageNav, $limitstart, $limit );
} //function

function editStadt( $Bid )
{
	global $database;
	global $HTML;
	if( $Bid > 0 )
	{ 
		$query = "SELECT `id_bereich` FROM `#__bindex_staedte` WHERE `id` = '$Bid'";
		$database->setQuery($query);
		$id_bereich = $database->loadResult();
		$HTML->formStadt( $Bid, $id_bereich );
	} else
	{ $HTML->formStadt();;
	}
} //function

function saveStadt( $Bid, $id_bereich )
{
	global $database, $Itemid;
	global $langActive;
	global $biConfig;
global $DBG;

	if( $Bid == 0 )
	{//neu speichern
		$name = mosGetParam( $_REQUEST, 'name_'.$biConfig->ini_language, '' );
		if( $name == '' ) return;

		$query = "INSERT INTO `#__bindex_staedte`" .
				"\n ( `published`, `id_bereich` )" .
				"\n VALUES" .
				"\n ( '1', '$id_bereich' )";
		$database->setQuery($query);
		$database->query();
$DBG->query($query);
		if ($database->getErrorNum())
		{ echo $database->stderr();
			return false;
		}
		
		$new_id = mysql_insert_id();
		$query = "INSERT INTO `#__bindex_staedte_data`" .
				"\n (" .
				"\n `id_stadt`" .
				"\n, `language`" .
				"\n, `value`" .
				"\n )" .
				"\n VALUES" .
				"\n (" .
				"\n '$new_id'" .
				"\n, '".$biConfig->ini_language."'" .
				"\n, '$name'" .
				"\n )";
		$database->setQuery($query);
		$database->query();
$DBG->query($query);
		if ($database->getErrorNum())
		{ echo $database->stderr();
			return false;
		}
			
		mosRedirect ("index.php?option=com_bindex&task=staedte&statmsg=gespeichert&Itemid=".$Itemid); 
		
	} else
	{//edit speichern
		if( $id_bereich > 0 )
		{
			$query = "UPDATE `#__bindex_staedte`" .
					"\n SET" .
					"\n `id_bereich` = '$id_bereich'" .
					"\n WHERE `id` = '$Bid'";
			$database->setQuery($query);
			$database->query();
$DBG->query($query);
		if ($database->getErrorNum())
		{ echo $database->stderr();
			return false;
		}
		} //if
		foreach( $langActive as $language )
		{
			$name = trim( mosGetParam( $_REQUEST, 'name_'.$language->code, '' ) );
			if( $name != '' )
			{
				$query = "SELECT * FROM `#__bindex_staedte_data`" .
						"\n WHERE `id_stadt` = '$Bid'" .
						"\n AND `language` = '".$language->code."'";
				$database->setQuery($query);
				$res = $database->loadResult();
$DBG->query($query);
				if( $res )
				{//bereits vorhanden
					$query = "UPDATE `#__bindex_staedte_data`" .
							"\n SET" .
							"\n `value` = '$name' " .
							"\n WHERE `id_stadt` = '$Bid'" .
							"\n AND `language` = '".$language->code."'";
					$database->setQuery($query);
					$database->query();
$DBG->query($query);
					if ($database->getErrorNum())
					{ echo $database->stderr();
						return FALSE;
					}
				} else
				{//neu
					$query = "INSERT INTO `#__bindex_staedte_data`" .
							"\n (" .
							"\n `id_stadt`" .
							"\n, `language`" .
							"\n, `value`" .
							"\n )" .
							"\n VALUES" .
							"\n (" .
							"\n '$Bid'" .
							"\n, '".$language->code."'" .
							"\n, '$name'" .
							"\n )";
					$database->setQuery($query);
					$database->query();
$DBG->query($query);
					if ($database->getErrorNum())
					{ echo $database->stderr();
						return FALSE;
					}
				} //if
			} //if
		}//foreach
		
		return TRUE;
	} //if
} //function

function showBIndexItem( $Bid, $popup = FALSE )
{
	global $database, $mosConfig_lang;
	global $biConfig, $HTML;
global $DBG;

	if( $Bid < 1 )
	{ echo "<h3>No id..</h3>";
		return;
	} else
	{
	
	$item = new bindex( $database );
	$item_ref = new bindex( $database );
		$query = "SELECT * FROM `#__bindex_items_data`" .
				"\n WHERE `id_item` = '$Bid'" .
				"\n AND `language` = '$mosConfig_lang'";
		$database->setQuery($query);
		$database->loadObject($item);
$DBG->query($query);
$DBG->out($item);
		$query = "SELECT * FROM `#__bindex_items_data`" .
				"\n WHERE `id_item` = '$Bid'" .
				"\n AND `language` = '$biConfig->ini_language'";
		$database->setQuery($query);
		$database->loadObject($item_ref);
$DBG->query($query);
$DBG->out($item_ref);
			if( !$item_ref->name)
			{ #no data ?? wrong id... :(((
				echo BI_NO_DATA;
				return FALSE;
			}// if
		} //if

		#load shared data
		$query = "SELECT `id_stadt`, `gmap_lat`, `gmap_lng` FROM `#__bindex_items`" .
					"\n WHERE `id` = '{$item_ref->id_item}'" .
					"\n LIMIT 1";
		$database->setQuery($query);
			
		$database->loadObject($item);
		$database->loadObject($item_ref);
$DBG->query($query);
$DBG->out($item);

		$HTML->bindexItem( $item, $item_ref, $popup );
		
} //function

function showBIndex( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt )
{
	global $database, $mainframe, $my, $Itemid, $mosConfig_list_limit, $mosConfig_lang;
	global $biConfig, $HTML;
global $DBG;
	
	$where = '';
	$where2 = '';

	if( $id_bereich > 0 )
	{
		$where2 = "\n AND `id_stadt` IN (" .
				"\n SELECT `id` FROM `#__bindex_staedte`" .
				"\n WHERE `id_bereich` = '".$id_bereich."')" .
				"\n ORDER BY `id_stadt`";
	} elseif( $id_stadt > 0)
	{
		$where2 = "\n AND `id_stadt` = '".$id_stadt."'";
	} //if

	$query = "SELECT *" .
			"\n FROM `#__bindex_items`" .
			"\n WHERE `published` = '1'"
			. $where2;

$DBG->query($query);

	/** query um den total zu finden */
	$database->setQuery($query);
	$tmp = $database->loadObjectList();
	if ($database->getErrorNum())
	{ echo $database->stderr();
		return;
	}
		
	$total = count($tmp);

	if ($limit == 0) $limit = $biConfig->list_limit;
	if ( $total <= $limit )
	{ $limitstart = 0;
	}

	/** the real query ;) */
	$database->setQuery( $query, $limitstart, $limit );
	$Bs = $database->loadObjectList();

	$items = array();
	foreach( $Bs as $dsatz )
	{
		$i = new bindex($database);
		$query = "SELECT * FROM `#__bindex_items_data`" .
				"\n WHERE `id_item` = '".$dsatz->id ."'".
				"\n AND `language` = '{$mosConfig_lang}'" .
				"\n LIMIT 1";
		$database->setQuery($query);
 		$database->loadObject($i);

		$i->id_stadt = $dsatz->id_stadt;
			$i->gmap_lat = $dsatz->gmap_lat;
			$i->gmap_lng = $dsatz->gmap_lng;
 		
//$DBG->out('ID:'.$i->id_stadt);
$DBG->query($query,1);

		$items[]  = $i;
		
	}//foreach
	
	if( $mosConfig_lang != $biConfig->ini_language )
	{//Viewing a foreign language so we also load the default for reference
		$items_ref = array();
		foreach( $Bs as $dsatz )
		{
			$i = new bindex($database);
			$query = "SELECT * FROM `#__bindex_items_data`" .
					"\n WHERE `id_item` = '".$dsatz->id."'" .
					"\n AND `language` = '".$biConfig->ini_language."'";
			$database->setQuery($query);
	 		$database->loadObject($i);

			$i->id_stadt = $dsatz->id_stadt;
			$i->gmap_lat = $dsatz->gmap_lat;
			$i->gmap_lng = $dsatz->gmap_lng;

			$items_ref[]  = $i;
$DBG->query($query);
		}//foreach
	}else
	{
		$items_ref = $items;
	} //if
	require_once( $biConfig->absolutePath . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );
//echo 'pagenav: '. $total, $limitstart, $limit.'<hr />';
	//$order[] = mosHTML::makeOption( 'filter', _ORDER_DROPDOWN_O );
	$order[] = mosHTML::makeOption( 'pat_id', 'Patientennummer' );
	$order[] = mosHTML::makeOption( 'name', 'Name' );

	$lists['order'] = mosHTML::selectList( $order, 'order', 'class="inputbox" size="1"  onchange="document.adminForm.submit();"', 'value', 'text', $selected );
	$lists['filter'] = $filter;
//$DBG->out($items);

	$HTML->bindex( $items, $items_ref, $pageNav, $lists, $selected );

}//function

function showBIndexTransStatus ( $limitstart, $limit, $filter, $selected,    $id_bereich, $id_stadt )
{
	global $database;
	global $biConfig, $HTML;
global $DBG;

	$query = "SELECT *" .
			"\n FROM `#__bindex_items`" .
			"\n WHERE `published` = '1'";

	$database->setQuery($query);
	
	$tmp = $database->loadObjectList();
	$total = count( $tmp );
	
//	if ($limit == 0) $limit = $biConfig->list_limit;
	$limit = $limit ? $limit : $biConfig->list_limit ;
	if ( $total <= $limit ) $limitstart = 0;

	/** the real query ;) */
	$database->setQuery( $query, $limitstart, $limit );
	$Bs = $database->loadObjectList();
$DBG->query($query);
//$DBG->out($Bs);
	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );

	$items = array();
	foreach( $Bs as $dsatz )
	{
//$DBG->out($dsatz);
//		$i = new bindex($database);
		$is = NULL;
		$query = "SELECT * FROM `#__bindex_items_data`" .
				"\n WHERE `id_item` = '".$dsatz->id ."'";
		$database->setQuery($query);
 		$is = $database->loadObjectList();

	//	$i->id_stadt = $dsatz->id_stadt;
 		
//$DBG->out('ID:'.$i->id_stadt);
$DBG->query($query);
//$DBG->out($is);
	//	foreach( $is as $i)
		$items[]  = $is;
		
	}//foreach
	$order[] = mosHTML::makeOption( 'pat_id', 'Patientennummer' );
	$order[] = mosHTML::makeOption( 'name', 'Name' );

	$lists['order'] = mosHTML::selectList( $order, 'order', 'class="inputbox" size="1"  onchange="document.adminForm.submit();"', 'value', 'text', $selected );
	$lists['filter'] = $filter;
	
	$HTML->bindexTrans( $items, $pageNav, $lists, $selected  );
	
} //function

function editBIndex( $id = 0, $from_task )
{
	global $database;
	global $HTML;
global $DBG;

	if( !$id )
	{ //neu
		$HTML->formBIndex();
	} else
	{ //bearbeiten
		$Bs = new bindex( $database );
		$query = "SELECT * FROM #__bindex_items_data" .
				"\n WHERE id_item = '" . $id . "'";
		$database->setQuery( $query );
		$Bs = $database->loadObjectList();
$DBG->query($query);
$DBG->out($Bs);
		$Bs_add = array();
		foreach( $Bs as $B )
		{
			$i = new bindex($database);
			$query = "SELECT * FROM `#__bindex_items`" .
					"\n WHERE `id` = '{$B->id_item}'" .
					"\n LIMIT 1";
			$database->setQuery($query);
			
			$database->loadObject($i);
$DBG->out($B);
//$DBG->out('B'.$B->id_stadt ." - I". $i->id_stadt);
			$B->id_stadt = $i->id_stadt;
			$B->gmap_lat = $i->gmap_lat;
			$B->gmap_lng = $i->gmap_lng;
			$Bs_add[] = $B;
$DBG->out($B);
$DBG->out('B'.$B->id_stadt ." - I". $i->id_stadt);
$DBG->query($query);
		} //foreach
$DBG->out($Bs);
$DBG->out($Bs_add);

		$HTML->formBIndex( $Bs_add, $from_task );
	}//if
}//function

function saveBindex ()
{
	global $database;
	global $langActive;
	global $biConfig;
global $DBG;
	#default language must always be present
	$test_name 		= mosGetParam( $_REQUEST, 'name_'.$biConfig->ini_language, '');
	if( $test_name == '' )
	{#SOMETHING WENT VERY WRONG...
		echo "<h2>No name !!</h2>";
		return FALSE;
	} //if
	
	$test_id 		= mosGetParam( $_REQUEST, 'bid_'.$biConfig->ini_language, '');
	if( $test_id == 0 )
	{
		#Brand NEW
		$BI = new bindex( $database );
		$BI->getFromRequest( $biConfig->ini_language );
$DBG->out('REQUEST');
		return( $BI->biStore()) ? TRUE : FALSE;
	} else
	{#SAVE ALL
		foreach( $langActive as $language )
		{
			$BI = new bindex( $database );
			$BI->getFromRequest( $language->code );
			$BI->id = $test_id;
$DBG->out($language->code);
$DBG->out('REQUEST');
			if( $BI->name != '' )
			{
				if( !$BI->biStore() ) return FALSE;
			} //if
		}//foreach
		return TRUE;
	} //if
} //function


function editConfig()
{
	global $biConfig, $HTML;
global $DBG;  
	
	$template = new bi_template(0,0,0);
	$lists['template_item'] = $template->getSelectList( 1 ); //item
	$lists['template_directory'] = $template->getSelectList( 2 );
	$lists['template_app'] = $template->getSelectList( 3 );

	$list = array();
	$list[] = mosHTML::makeOption( '24', '24');
	$list[] = mosHTML::makeOption( '48', '48');
	$list[] = mosHTML::makeOption( '48a', '48a');
	$lists['icon_set'] = mosHTML::selectList( $list, 'icon_set', 'class="inputbox"', 'value', 'text', $biConfig->iconSet);

	$list = array();
	$list[] = mosHTML::makeOption( 'icons',	BI_BUTTONS_SHOW_ICONS );
	$list[] = mosHTML::makeOption( 'text',	BI_BUTTONS_SHOW_TEXT );
	$list[] = mosHTML::makeOption( 'both',	BI_BUTTONS_SHOW_BOTH );
	$lists['buttons_show'] = mosHTML::selectList( $list, 'buttons_show', 'class="inputbox"', 'value', 'text', $biConfig->buttonsShow);

	$list = array();
	$list[] = mosHTML::makeOption( '1', "On" );
	$list[] = mosHTML::makeOption( '0', "Off" );
	$lists['DEBUG'] = mosHTML::selectList( $list, 'DEBUG', 'class="inputbox" size="2"', 'value', 'text', $biConfig->DEBUG );
	$lists['LOG'] = mosHTML::selectList( $list, 'LOG', 'class="inputbox" size="2"', 'value', 'text', $biConfig->LOG );

	$list = array();
	$list[] = mosHTML::makeOption( 'list', BI_VIEW_LIST );
	$list[] = mosHTML::makeOption( 'directory', BI_VIEW_DIRECTORY );
	$lists['view_mode']= mosHTML::selectList( $list, 'view_mode', 'class="inputbox" size="1"', 'value', 'text', $biConfig->viewMode );
	
	$HTML->formConfig( $lists );

} //function


?>
